const ErrorCode = require('../utils/ErrorCode')
const ResultVo = require('../utils/ResultVo')
const Exception = require('../exception/Exception')
const ArticleService = require('../service/ArticleService')

class ArticleController {
  async articleList(req, res, next) {
    const { page, size } = req.query
    const result = await ArticleService.getArticle(Number(page), Number(size)).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async createArticle(req, res, next) {
    const data = req.body
    await ArticleService.save(data).catch(err => next(err))
    res.json(ResultVo.successNull())
  }

  async articleDetail(req, res, next) {
    const { id } = req.params
    const result = await ArticleService.findArticleDetail(id).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async searchArticle(req, res, next) {
    const { title, page, size } = req.body
    const result = await ArticleService.findByName(title, Number(page), Number(size)).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async likeArticle(req, res, next) {
    const { userId, articleId } = req.body
    const result = await ArticleService.addZan(userId, articleId).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async disLikeArticle(req, res, next) {
    const { userId, articleId } = req.body
    const result = await ArticleService.cancelZan(userId, articleId).catch(err => next(err))
    res.json(ResultVo.success(result))
  }

  async getUserArticle(req, res, next) {
    const { userId, page, size } = req.body
    const result = await ArticleService.findByUser(userId, Number(page), Number(size)).catch(err => next(err))
    res.json(ResultVo.success(result))
  }
}

module.exports = new ArticleController()